﻿@*
    For more information on enabling MVC for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860
*@
@{
    Layout = null;
}
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>Demo</title>
    <!-- 请勿在项目正式环境中引用该 layui.css 地址 -->
    @*    <script src="~/scripts/jquery-3.4.1.min.js"></script> *@
    <link href="~/libs/layui-v2.5.6/layui/css/layui.css" rel="stylesheet" />
    <script src="~/libs/layui-v2.9.17/layui/layui.js"></script>
    <script src="~/libs/jquery/jquery.js"></script>
    @*     <script src="~/layui-v2.5.6/layui/layui.js"></script> *@
</head>
<body>
    <form class="layui-form layui-row layui-col-space16">
        <div class="layui-col-md2">
            <div class="layui-input-wrap">
                <input type="text" name="vendorcode" value="" placeholder="请输入供应商编码" class="layui-input " lay-affix="clear" id="vendorcode">
            </div>
        </div>
        <div class="layui-col-md2">
            <div class="layui-input-wrap">
                <input type="text" name="vendorname" value="" placeholder="请输入供应商名称" class="layui-input" lay-affix="clear" id="vendorname">
            </div>
        </div>
        <div class="layui-col-md2">
            <div class="layui-input-wrap">
                <input type="text" name="vendornick" placeholder="请输入供应商简称" lay-affix="clear" class="layui-input" id="vendorNick">
            </div>
        </div>
        <div class="layui-col-md2">
            <div class="layui-input-wrap">
                <input type="text" name="vendoren" placeholder="请输入供应商英文名称" lay-affix="clear" class="layui-input" id="vendoren">
            </div>
        </div>
        <div class="layui-btn-container layui-col-xs12">
            <button class="layui-btn layui-bg-blue" lay-filter="demo-table-search">搜索</button>
            <button type="reset" class="layui-btn layui-btn-primary">重置</button>
            <button type="button" class="layui-btn layui-bg-blue" lay-on="test-page-custom">添加</button>
        </div>
    </form>
    <table class="layui-hide" id="demo" lay-filter="test">
    </table>
    <script src="~/libs/layui-v2.5.6/layui/layui.js"></script>
    <!--操作栏模板-->
    <script type="text/html" id="TPL-treeTable-demo-tools">
        <div class="layui-clear-space">
          <a class="layui-btn layui-btn-xs layui-bg-orange" lay-event="edit">修改</a>
          <a class="layui-btn layui-btn-xs layui-btn-danger" lay-event="dels">删除</a>
        </div>
    </script>
    @*添加*@
    <script>
        layui.use(function () {
            var $ = layui.$;
            var layer = layui.layer;
            var treeTable = layui.treeTable;
            var util = layui.util;
            var form = layui.form;
            // 事件
            util.on('lay-on', {
                'test-page-custom': function () {
                    layer.open({
                        type: 2,
                        area: ['500px', '400px'],
                        resize: false,
                        shadeClose: true,
                        title: '添加供应商信息',
                        content: "https://localhost:44394/ChuBoWen/AddMdVendorView",
                        btn: ['确定', '取消'],
                        yes: function (index, layero) {
                            var data = layer.getChildFrame('body', index).find('form').serialize();
                            $.ajax({
                                url: "/ChuBoWen/AddMdVendor",
                                type: "get",
                                async: true,
                                data: data,
                                contentType: "application/json;charset=UTF-8",
                                success: res => {
                                    if (res > 0) {
                                        alert("添加成功")
                                        //关闭弹出层
                                        layer.closeAll();
                                        //刷新表格
                                        layer.msg(data.msg, { icon: 1, time: 100 }, function () {
                                            setTimeout('window.location.reload()', 100);
                                        });
                                    }
                                    else {
                                        console.log(res.code)
                                        alert("添加失败,该用户名已存在")
                                    }
                                }
                            })
                            return false; // 阻止默认 form 跳转
                        }
                    });
                },
            });
        });
    </script>
    @* 显示 *@
    <script>
        layui.use(['table', 'form', 'layer', 'dropdown', 'jquery'], function () {
            var table = layui.table;
            var form = layui.form;
            var dropdown = layui.dropdown;
            var layer = layui.layer
            var datas = [];
            const $ = layui.$;
            // 创建表格实例
            // 渲染
            var inst = table.render({
                elem: '#demo',
                url: '/ChuBoWen/GetMdVendorList',
                cols: [[
                    { field: 'vendorCode', width: 200, title: '供应商编码', sort: true },
                    { field: 'vendorName', width: 250, title: '供应商名称' },
                    { field: 'vendorNick', width: 150, title: '供应商简称', },
                    { field: 'vendorEn', width: 150, title: '供应商英文名称' },
                    { field: 'vendorScore', width: 150, title: '供应商评分', sort: true },
                    { field: 'tel', width: 150, title: '供应商电话' },
                    { field: 'remark', width: 150, title: '供应商备注' },
                    { field: 'writes', title: '操作', width: 200, toolbar: "#TPL-treeTable-demo-tools" }
                ]],
                page: {
                    limit: 10,
                    limits: [10, 20, 30, 40, 50],
                    height: 520,
                    prev: '上一页',
                    next: '下一页',
                    first: '首页',
                    last: '尾页'
                },
                request: {//分页请求参数
                    pageName: 'pageIndex',
                    limitName: 'pageSize'
                },
                parseData: function (res) {//获取数据
                    console.log(res)
                    return {
                        "code": res.code, //解析接口状态
                        "count": res.data.totalCount, //解析数据长度
                        "data": res.data.pageList//解析数据列表

                    };
                },
            });
            // 搜索提交
            form.on('submit(demo-table-search)', function (data) {
                var field = data.field; // 获得表单字段
                // 执行搜索重载
                table.reload('demo', {
                    page: {
                        curr: 1 // 重新从第 1 页开始
                    },
                    where: field // 搜索的字段
                });
                layer.msg('搜索成功');
                return false; // 阻止默认 form 跳转
            });

            // 单元格工具事件
            table.on('tool(test)', function (obj) {
                var layEvent = obj.event; // 获得 lay-event 对应的值
                var trData = obj.data;
                var tableId = obj.config.id;
                if (layEvent === "dels") {
                    console.log(trData.id);
                    layer.confirm("确定删除吗？", function (index) {
                        $.ajax({
                            url: "/ChuBoWen/DeleteMdVendor",
                            type: "get",
                            async: true,
                            data: { id: trData.id },
                            success: res => {
                                if (res > 0) {
                                    layer.msg('删除成功');
                                } else {
                                    layer.msg('删除失败');
                                }
                            }
                        });
                        obj.del(); // 删除当前行
                        layer.close(index); // 关闭弹出层
                    });
                }
                else if (layEvent === "edit") {
                    // 打开编辑页面
                    layer.open({
                        type: 2,
                        area: ['500px', '400px'],
                        resize: false,
                        shadeClose: true,
                        title: '修改供应商信息',
                        content: '/ChuBoWen/UpdMdVendorView?id=' + trData.id,
                        btn: ['确定', '取消'],
                        yes: function (index, layero) {
                            var data = layer.getChildFrame('body', index).find('form').serialize();
                            $.ajax({
                                url: '/ChuBoWen/UpdateMdVendor',
                                type: "get",
                                async: true,
                                data: data,
                                contentType: "application/json;charset=UTF-8",
                                success: res => {
                                    if (res != null) {
                                        alert("修改成功")
                                        //关闭弹出层
                                        layer.closeAll();
                                        //刷新表格
                                        layer.msg(data.msg, { icon: 1, time: 100 }, function () {
                                            setTimeout('window.location.reload()', 100);
                                        });
                                    }
                                    else {
                                        console.log(res.code)
                                        alert("修改失败")
                                    }
                                }
                            })
                            return false; // 阻止默认 form 跳转
                        }
                    });
                }
            });
        });
    </script>

</body>
</html>

